package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.dao.AdminDao;
import com.example.dao.ReservestudyDao;
import com.example.dao.StudyDao;
import com.example.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 日期：2025/5/11 19:57
 */

@Service
public class ReservestudyService {

    @Resource
    private ReservestudyDao reservestudyDao;

    @Resource
    private AdminDao adminDao;
    @Resource
    private StudyDao studyDao;


    public PageInfo<Reservestudy> findBySearch(Params params) {
        //        开启分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
//        接下来的查询会自动按照当前开启的分页设置来查询
        List<Reservestudy> list = reservestudyDao.findBySearch(params);
        if (CollectionUtils.isEmpty(list)) {
            return PageInfo.of(new ArrayList<>());
        }
        for (Reservestudy reservestudy : list) {
            if (ObjectUtil.isNotEmpty(reservestudy.getStudyId())) {
                Study study = studyDao.selectByPrimaryKey(reservestudy.getStudyId());
                if (ObjectUtil.isNotEmpty(study)) {
                    reservestudy.setStudyName(study.getName());;
                }
            }
            if (ObjectUtil.isNotEmpty(reservestudy.getUserId())) {
                Admin admin = adminDao.selectByPrimaryKey(reservestudy.getUserId());
                if (ObjectUtil.isNotEmpty(admin)) {
                    reservestudy.setUserName(admin.getName());
                }
            }
        }
        return PageInfo.of(list);
    }


        public void add (Reservestudy reservestudy){
                reservestudyDao.insertSelective(reservestudy);
        }

        public void update (Reservestudy reservestudy){
            reservestudyDao.updateByPrimaryKeySelective(reservestudy);
        }

        public void delete (Integer id){
            reservestudyDao.deleteByPrimaryKey(id);
        }

}
